package com.kaifamiao.demo.d0809;

import java.util.Arrays;
import java.util.Comparator;

public class Demo02 {

    public static void main(String[] args) {
        Student stu = new Student();
        stu.setName("杨红强");
        stu.setScore(91);

        Student stu1 = new Student();
        stu1.setScore(50);
        stu1.setName("刘凯");

        Student stu2 = new Student();
        stu2.setName("魏域江");
        stu2.setScore(65);

        Student[] stus = {stu, stu1, stu2};


//        Arrays.sort(stus, new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                return o1.getScore() - o2.getScore();
//            }
//        });

        sort(stus, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getScore() - o2.getScore();
            }
        });

        System.out.println(Arrays.toString(stus));
    }

    public static void sort(Student[] students, Comparator<Student> comparator) {
        for (int i = 0; i < students.length - 1; i++) {

            for (int j = 0; j < students.length - 1 - i; j ++){
                if (comparator.compare(students[j], students[j + 1]) > 0){
                    Student p = students[j];
                    students[j] = students[j + 1];
                    students[j + 1] = p;
                }

            }
        }
    }
}

